package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.Encyclo;
import cn.b.training.baby_education_training.entity.FoodTag;
import cn.b.training.baby_education_training.entity.Pregnancy;
import cn.b.training.baby_education_training.entity.ScienceTag;
import cn.b.training.baby_education_training.repository.EncycloRepository;
import cn.b.training.baby_education_training.repository.FoodTagRepository;
import cn.b.training.baby_education_training.repository.PrenatalRepository;
import cn.b.training.baby_education_training.repository.ScienceTagRepository;
import cn.b.training.baby_education_training.service.EncycloService;
import cn.b.training.baby_education_training.service.dto.EncycloDto;
import cn.b.training.baby_education_training.service.dto.EncyclopediaDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-02 14:40
 **/
@Service
public class EncycloServiceImpl implements EncycloService {
    @Autowired
    private PrenatalRepository prenatalRepository;
    @Autowired
    private ScienceTagRepository scienceTagRepository;
    @Autowired
    private FoodTagRepository foodTagRepository;
    @Autowired
    private BaiduAiService baiduAiService;
    @Autowired
    private EncycloRepository encycloRepository;
    @Override
    public boolean addEncyclo(EncycloDto dto) {
        Pregnancy prenatal = prenatalRepository.findPrenatalById(dto.getPrenatalId());
        ScienceTag scienceTag = scienceTagRepository.findScienceTagById(dto.getScienceTagId());
        FoodTag foodTag = foodTagRepository.findFoodTagById(dto.getFoodTagId());
        Encyclo encyclo = new Encyclo();
        if (dto.getEncycloCover() != null) {
            String fileKey = UUID.randomUUID().toString();
            String fileName = dto.getEncycloCover().getOriginalFilename();
            if (fileName.indexOf(".") > 0) {
                String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
                fileKey += extName;
            }
            try {
                baiduAiService.putFile1(fileKey, dto.getEncycloCover().getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            encyclo.setEncycloCover("http://hzw-bos.bj.bcebos.com/encyclopedia/" + fileKey);
        }
        encyclo.setPregnancyId(prenatal.getId());
        encyclo.setScienceTagId(scienceTag.getId());
        encyclo.setFoodTagId(foodTag.getId());
        encyclo.setEncycloTitle(dto.getEncycloTitle());
        encyclo.setEncycloDetail(dto.getEncycloDetail());
        encyclo.setEncycloVisit(dto.getEncycloVisit());
        encyclo.setEncycloLike(dto.getEncycloLike());
        encyclo = encycloRepository.save(encyclo);
        if (encyclo!=null){
            return true;
        }
        return false;
    }

    @Override
    public EncyclopediaDto getEncycloById(Long id) {
        Encyclo encyclo = encycloRepository.getByEncycloId(id);
        EncyclopediaDto dto = new EncyclopediaDto();
        dto.setId(encyclo.getId());
        dto.setEncycloTitle(encyclo.getEncycloTitle());
        dto.setEncycloDetail(encyclo.getEncycloDetail());
        dto.setEncycloVisit(encyclo.getEncycloVisit());
        dto.setEncycloLike(encyclo.getEncycloLike());
        dto.setEncycloCover(encyclo.getEncycloCover());
        dto.setPregnancy(prenatalRepository.findPrenatal(encyclo.getPregnancyId()));
        dto.setScienceTag(scienceTagRepository.findScienceTag(encyclo.getScienceTagId()));
        dto.setFoodTag(foodTagRepository.findFoodTag(encyclo.getFoodTagId()));
        return dto;
    }

    @Override
    public List<Encyclo> getEncycloByTitle(String encycloTitle) {
        return encycloRepository.getEncycloByTitle(encycloTitle);
    }

    @Override
    public Page<Encyclo> getAllEncyclos(Integer page, Integer size) {
        return this.encycloRepository.findAll(PageRequest.of(page,size));
    }

    @Override
    public Boolean deleteEncycloById(Long id) {
        try{
            this.encycloRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
